import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from "vite-plugin-electron"
import electronRenderer from "vite-plugin-electron-renderer"
import polyfillExports from "vite-plugin-electron-renderer"
import { fileURLToPath, URL } from 'node:url'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({ mode }) => ({
  base: mode == 'development' ? '' : './',
  plugins: [
    vue(),
    AutoImport({
      imports: [
        'vue',
        {
          'naive-ui': [
            'useDialog',
            'useMessage',
            'useNotification',
            'useLoadingBar'
          ]
        }
      ]
    }),
    Components({
      resolvers: [NaiveUiResolver()]
    }),
    electron([{
      entry: "electron.ts" // 主进程文件
    },
      {
        entry: 'preload.ts'
      }
    ]),
    electronRenderer(),
    polyfillExports()
  ], resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
      // "@": path.resolve(__dirname, "./src"),
      // "_u": path.resolve(__dirname, "./src/utils"),
      // "_utils": path.resolve(__dirname, "./src/utils"),
      // "_views": path.resolve(__dirname, "./src/views"),
      // "_db": path.resolve(__dirname, "./src/dbIndex"),
    }
  },
  server: {
    port: 16282
  },
  build: {
    emptyOutDir: false, // 默认情况下，若 outDir 在 root 目录下，则 Vite 会在构建时清空该目录
    outDir: 'dist-electron', // 指定构建输出路径为dist/suzhou-assets
    assetsDir: 'dist-electron', // 指定静态资源文件夹名称为suzhou-assets
    chunkSizeWarningLimit: 5000, //打包最大大小
    rollupOptions: {
      output: {
        manualChunks: {
          vue: ['vue'],
          // 'element-plus': ['element-plus']
        }
      }
    }
  }
}))